#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
from db.connect_db import engine,Students,Teacher,Classes,Class_choise,Class_record,Homework,Grade,Admin
from sqlalchemy.orm import sessionmaker
session_connet = sessionmaker(bind=engine)
session = session_connet()
class Admin_run(object):
    def __init__(self,user):
        self.user = user
    def menu(self):
        #print('管理员',self.user)
        choise_menu = '''
                \033[1;31m
                   ---------Welcome  %s----------
                   1. 添加学生
                   2. 添加教师
                   3. 退出
                \033[0m

                ''' % self.user

        menu_dict = {
            '1': Admin_run.add_student,
            '2': Admin_run.add_teacher,
            '3': 'exit'
        }

        while True:
            print(choise_menu)
            choise = input('请选择>>>')
            if choise in menu_dict:
                if choise == '3':
                    exit()
                else:
                    menu_dict[choise](self)
            else:
                print('输入错误！')

    def add_student(self):
        student_qq = input('请输入学生qq>>>').strip()
        student_name = input('请输入学生姓名>>>').strip()
        student_passwd = input('请输入学生密码>>>').strip()
        if student_qq.isdigit() and len(student_name)!=0 and len(student_passwd)!=0 :
            obj = session.query(Students).filter(Students.qq==student_qq).first()
            if obj is None: #如果返回为空说明没有重复账号，可以进行创建
                obj_add = Students(qq = student_qq,name=student_name,passwd=student_passwd)
                session.add(obj_add)
                session.commit()
                print('创建成功！')
        else:
            print('输入错误！')

    def add_teacher(self):
        teacher_name = input('请输入教师姓名>>>').strip()
        teacher_passwd = input('请输入密码>>>').strip()
        if len(teacher_name)!=0 and len(teacher_passwd)!=0:
            obj = session.query(Teacher).filter(Teacher.name == teacher_name).first()
            if obj is None:#如果返回为空说明没有重复账号，可以进行创建
                obj_add = Teacher(name=teacher_name,passwd=teacher_passwd)
                session.add(obj_add)
                session.commit()
                print('创建成功！')